package com.lagou.rpc.provider2.tool;

import org.I0Itec.zkclient.IZkChildListener;
import org.I0Itec.zkclient.ZkClient;

import java.util.List;

public class ZkCli02 {

    private ZkClient zkClient = null;
    private static String P_NODE = "/base";

    public ZkCli02() {
        Connect();
    }

    public void Connect() {
        zkClient = new ZkClient("127.0.0.1:2181");
        if (!zkClient.exists(P_NODE)) {
            zkClient.createPersistent(P_NODE);
        }
    }

    public void creatNode(String ip, int port) {

        String path = P_NODE + "/" + ip + "#" + port;

        zkClient.createEphemeral(path);

        zkClient.writeData(path, ip + "#" + port);

        zkClient.subscribeChildChanges(P_NODE, new IZkChildListener() {
            @Override
            public void handleChildChange(String s, List<String> list) throws Exception {
            }
        });
    }

}
